package com.vvlive.caibao.smartcity.ui;

import android.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.WorkOvertimeApply;
import com.vvlive.caibao.smartcity.utils.TimeUtil;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.DateTimePickerDialog;

import org.kymjs.kjframe.ui.BindView;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/3.
 * 加班登记
 */
public class WorkOvertimeApplyActivity extends BaseActivity {

    @BindView(id = R.id.wo_input)
    EditText ETinput;

    @BindView(id = R.id.wo_set_start_time, click = true)
    RelativeLayout RLsetStartTime;
    @BindView(id = R.id.wo_set_end_time, click = true)
    RelativeLayout RLsetEndTime;

    @BindView(id = R.id.wo_show_start_time)
    TextView TVshowStartTime;
    @BindView(id = R.id.wo_show_end_time)
    TextView TVshowEndTime;

    @BindView(id = R.id.wo_submit, click = true)
    Button BTNsubmit;

    @BindView(id = R.id.wo_about, click = true)
    TextView TVabout;

    private String reason;
    private String startTime;
    private String endTime;

    private long startTimeMillis;
    private long endTimeMillis;

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_work_overtime;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setMoreVisible(false);
        actionBar.setTitle("加班登记");
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.wo_set_start_time:
                // 设置加班开始时间;
                setStartTime();
                break;
            case R.id.wo_set_end_time:
                // 设置加班结束时间；
                setEndTime();
                break;
            case R.id.wo_submit:
                // 提交加班申请
                submitApply();
                break;
            case R.id.wo_about:
                // 加班说明
                alert("加班没有加班费哦。");
                break;
        }
    }

    /**
     * 设置加班开始的时间
     */
    private void setStartTime() {
        DateTimePickerDialog dialog = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
            @Override
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                startTimeMillis = date;
                startTime = TimeUtil.getTargetDateTime(date);
                TVshowStartTime.setText(startTime);
            }
        });
        dialog.show();
    }

    /**
     * 设置加班结束的时间
     */
    private void setEndTime() {
        DateTimePickerDialog dialog = new DateTimePickerDialog(this, System.currentTimeMillis());
        dialog.setOnDateTimeSetListener(new DateTimePickerDialog.OnDateTimeSetListener() {
            @Override
            public void OnDateTimeSet(AlertDialog dialog, long date) {
                endTimeMillis = date;
                endTime = TimeUtil.getTargetDateTime(date);
                TVshowEndTime.setText(endTime);
            }
        });
        dialog.show();
    }

    /**
     * 提交加班的申请
     */
    private void submitApply() {
        reason = ETinput.getText().toString();
        if (canApply()) {
            alert(new WorkOvertimeApply(reason, startTime, endTime));
        } else {
            alert("请完善加班信息");
        }
    }

    private boolean canApply() {
        return !TextUtils.isEmpty(reason) && !TextUtils.isEmpty(startTime) && !TextUtils.isEmpty(endTime) ? true : false;
    }
}
